//
//  MSAreaViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/4.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSAreaViewController.h"
#import "MSAreaViewTableVC.h"

@interface MSAreaViewController ()
@property (nonatomic, copy) NSString            *areaFile;
@property (nonatomic, copy) NSString            *codeFile;
@property (nonatomic, strong) NSMutableArray    *codeArray;     //数据
@property (nonatomic, strong) NSMutableArray    *stateArray;
@property (nonatomic, strong) NSMutableArray    *dataArray;     //数据
@property (nonatomic, strong) MSAreaViewTableVC *tableVC;
@end

@implementation MSAreaViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    rightBarBtn.hidden = NO;
    rightBarBtn.frame = CGRectMake(SCREENWIDTH-50, 20, 40, 44);;
    [rightBarBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    rightBarBtn.backgroundColor = [UIColor blueColor];
    rightBarBtn.titleLabel.font = HeitiFont(14);
    
    GXButton *reSetBtn = [GXButton buttonWithType:UIButtonTypeCustom];
    reSetBtn.frame = CGRectMake(SCREENWIDTH-90, 20, 40, 44);
    [reSetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [reSetBtn setTitleColor:kCyanColor forState:UIControlStateNormal];
    reSetBtn.titleLabel.font = HeitiFont(14);
    [reSetBtn addTarget:self action:@selector(reSetClick) forControlEvents:UIControlEventTouchUpInside];
    [_navBarView addSubview:reSetBtn];
//    reSetBtn.backgroundColor = [UIColor yellowColor];
    titleLable.text = @"地区";
}

- (void)initUI{
    self.tableVC = [[MSAreaViewTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
}

- (void)testDataFunc{
    _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    _codeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dict in netEngine.chinaAreaArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            NSArray *tempArray = [HDUtil checkArray:dict[@"provinceList"]];
            NSMutableArray *proviencesArray = [NSMutableArray arrayWithCapacity:0];
            NSMutableArray *proviencesCodeArray = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *provinceDict in tempArray) {
                if ([provinceDict isKindOfClass:[NSDictionary class]]){
                    [proviencesArray addObject:[HDUtil check:provinceDict[@"name"]]];
                    [proviencesCodeArray addObject:[HDUtil check:provinceDict[@"code"]]];
                }
            }
            NSDictionary *chinaDict = @{[HDUtil check:dict[@"name"]]:proviencesArray};
            NSDictionary *chinaCodeDict = @{[HDUtil check:dict[@"code"]]:proviencesCodeArray};
            [_dataArray addObject:chinaDict];
            [_codeArray addObject:chinaCodeDict];
        }
    }
    
//    _dataArray = [NSMutableArray arrayWithArray:
//                                @[@{@"东北":@[@"辽宁",@"吉林",@"黑龙江"]},
//                                
//                                @{@"华北":@[@"北京",@"河北",@"天津",@"山西",@"内蒙古"]},
//                                
//                                @{@"华东":@[@"上海",@"山东",@"江苏",@"安徽",@"福建",@"江西"]},
//                                
//                                @{@"华中":@[@"河南",@"湖北",@"湖南"]},
//                                
//                                @{@"西北":@[@"山西",@"甘肃",@"青海",@"宁夏",@"新疆"]},
//                                
//                                @{@"华南":@[@"广东",@"广西",@"海南"]},
//                                
//                                @{@"西南":@[@"重庆",@"四川",@"贵州",@"云南",@"西藏"]},]
//                              ];
//    _codeArray = _dataArray;
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    self.areaFile = [documentsPath stringByAppendingPathComponent:@"preferenceAreaFile.plist"];
    self.codeFile = [documentsPath stringByAppendingPathComponent:@"areaCodeFile.plist"];

    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:self.areaFile]) {
        _stateArray = [[NSMutableArray alloc] initWithContentsOfFile:self.areaFile];
    }
    else{
        _stateArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i = 0; i<_dataArray.count; i++) {
            NSMutableArray *rowArray = [[NSMutableArray alloc] initWithCapacity:0];
            NSDictionary *dict = _dataArray[i];
            NSArray *tempArray = [[dict allValues] objectAtIndex:0];
            for (int j = 0;j<tempArray.count; j++ ) {
                [rowArray addObject:@"NO"];
            }
            [_stateArray addObject:rowArray];
        }
    }
    self.tableVC.stateArray = _stateArray;
    self.tableVC.dataArray = _dataArray;
}

- (void)rightBarBtnAction{
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i<self.stateArray.count; i++) {
        NSMutableArray *rowArray = [self.stateArray objectAtIndex:i];
        if (![rowArray containsObject:@"NO"]) {
            NSDictionary *dict = _dataArray[i];
            [relutArray addObject:[[dict allKeys] objectAtIndex:0]];
            
            NSDictionary *codeDict = _codeArray[i];
            NSArray *codeArray = [[codeDict allValues] objectAtIndex:0];
            for (int j = 0; j<codeArray.count; j++ ){
                [tempCodeArray addObject:codeArray[j]];
            }
        }
        else{
            for (int j = 0; j<rowArray.count; j++ ) {
                NSDictionary *dict = _dataArray[i];
                NSArray *titleArray = [[dict allValues] objectAtIndex:0];
                
                NSDictionary *codeDict = _codeArray[i];
                NSArray *codeArray = [[codeDict allValues] objectAtIndex:0];
                
                NSString *stateStr = rowArray[j];
                if ([stateStr isEqualToString:@"YES"]) {
                    [relutArray addObject:titleArray[j]];
                    [tempCodeArray addObject:codeArray[j]];
                }
            }
        }
    }
    [_stateArray writeToFile:self.areaFile atomically:YES];
    [tempCodeArray writeToFile:self.codeFile atomically:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:KPreferenceAreaChange object:self];

    if (relutArray.count) {
        if (self.ChoicBlock) {
            self.ChoicBlock(relutArray);
        }
    }
    [rootNavigation popViewControllerAnimated:YES];
}

- (void)reSetClick{
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    if (self.ChoicBlock) {
        self.ChoicBlock(relutArray);
    }
    [self.tableVC resetState];
    [_stateArray writeToFile:self.areaFile atomically:YES];
    [@[] writeToFile:self.codeFile atomically:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:KPreferenceAreaChange object:self];
}

@end
